//
//  OIHexFormatter.m
//  OrchIDE
//
//  Created by local22 on 8/13/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "OIHexFormatter.h"


@implementation OIHexFormatter
- (NSString *)hexFormat
{
	return @"%04hx";
}

- (NSString *)stringForObjectValue:(id)value
{
	if(![value respondsToSelector:@selector(intValue)]) {
		return nil;
	}
	
	int32_t rawValue = [value intValue];
	return [NSString stringWithFormat:[self hexFormat], rawValue];
}

- (BOOL)getObjectValue:(id *)obj forString:(NSString *)string errorDescription:(NSString **)errorDescription
{
	unsigned rawValue;
	
	NSScanner *scanner = [NSScanner scannerWithString:string];
	if([scanner scanHexInt:(unsigned *)&rawValue] && [scanner isAtEnd]) {
		if(obj) {
			*obj = [NSNumber numberWithInt:rawValue];
		}
		return YES;
	} else {
		if(errorDescription) {
			*errorDescription = @"Invalid value";
		}
	}
	
	return NO;
	
}
@end
